package com.zhiche.lisa.bms.controller.driver;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.pojo.vo.driver.RouteVO;
import com.zhiche.lisa.bms.service.driver.IRouteService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 线路表
 * </p>
 *
 * @author liangpeng
 * @since 2018-08-30
 */
@RestController
@RequestMapping("/route")
@Api(description = "线路维护", tags = {"线路维护"})
@Slf4j
public class RouteController {

    @Autowired
    private IRouteService routeService;

    @PostMapping(value = "/page_list")
    @ApiOperation(value = "分页获得线路", notes = "分页获得线路",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RouteVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<RouteVO>> listByPage(@ApiParam(name = "RouteVO", value = "线路", required = true) @RequestBody Page<RouteVO> page) {
        log.info("RouteController.listByPage params:{}", page);
        RestfulResponse<Page<RouteVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<RouteVO> pageVo = routeService.listByPage(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            log.error("RouteController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    /**
     * 添加新线路
     *
     * @param routeVO
     * @param br
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加新线路", notes = "添加新线路",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Integer.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Integer> add(@ApiParam(name = "RouteVO", value = "线路VO", required = true) @Valid @RequestBody RouteVO routeVO, BindingResult br) {
        log.info("RouteController.add params: {}", routeVO);
        RestfulResponse<Integer> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Integer id = routeService.add(routeVO, br);
            result.setData(id);
        } catch (BaseException e) {
            log.error("RouteController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 更新线路信息
     *
     * @param routeVO
     * @param br
     * @return
     */
    @PostMapping(value = "/update")
    @ApiOperation(value = "更新线路信息", notes = "更新线路信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> update(@ApiParam(name = "RouteVO", value = "线路VO", required = true) @Valid @RequestBody RouteVO routeVO, BindingResult br) {
        log.info("RouteController.update params: {}", routeVO);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = routeService.update(routeVO, br);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("RouteController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 设置线路是否可用
     *
     * @param id
     * @param enableOrDisabled
     * @return
     */
    @PostMapping(value = "/enableOrDisabled")
    @ApiOperation(value = "设置线路是否可用", notes = "设置线路是否可用",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "Integer", name = "id", value = "线路ID", required = true), @ApiImplicitParam(paramType = "form", dataType = "Boolean", name = "enableOrDisabled", value = "启用和禁用状态", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> enableOrDisabled(Integer id, Boolean enableOrDisabled) {
        log.info("RouteController.enableOrDisabled params: {}, {}", id, enableOrDisabled);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = routeService.enableOrDisabled(id, enableOrDisabled);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("RouteController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 根据ID得到线路信息
     *
     * @param id
     * @return
     * @throws
     */
    @GetMapping("/getById")
    @ApiOperation(value = "根据ID得到线路信息", notes = "根据ID得到线路信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "线路ID", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RouteVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<RouteVO> getById(Integer id) {
        log.info("RouteController.getById params: {}", id);
        RestfulResponse<RouteVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(routeService.getById(id));
        } catch (BaseException e) {
            log.error("RouteController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 删除线路信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/del")
    @ApiOperation(value = "根据ID得到线路信息", notes = "根据ID得到线路信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "Integer", name = "id", value = "线路ID", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> del(Integer id) throws Exception {
        log.info("RouteController.del params: {}", id);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = routeService.del(id);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("RouteController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getOtgsByName")
    @ApiOperation(value = "根据关键字得到起点信息", notes = "根据关键字得到起点信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "keyword", value = "关键字", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = String.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> getOtgByName(String keyword) {
        log.info("RouteController.getOtgByName params: {}", keyword);
        RestfulResponse<List<String>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(routeService.getOTagsByKey(keyword));
        } catch (BaseException e) {
            log.error("RouteController.getOtgByName ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.getOtgByName ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getDtgsByName")
    @ApiOperation(value = "根据关键字得到终点区域信息", notes = "根据关键字得到终点区域信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "keyword", value = "关键字", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = String.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> getDtgByName(String keyword) {
        log.info("RouteController.getOtgByName params: {}", keyword);
        RestfulResponse<List<String>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(routeService.getDTagsByKey(keyword));
        } catch (BaseException e) {
            log.error("RouteController.getOtgByName ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.getOtgByName ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "根据关键字得到线路信息", notes = "根据关键字得到线路信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "keyword", value = "关键字", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RouteVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<RouteVO>> list(String keyword) throws Exception {
        log.info("RouteController.list params: {}", keyword);
        RestfulResponse<List<RouteVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(routeService.listByKeyword(keyword));
        } catch (BaseException e) {
            log.error("RouteController.getOtgByName ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.getOtgByName ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}

